home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / ARexxTools / RxilShell.lha / RxilShell / rxil_fix / check_port.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-01  |  9.4 KB  |  457 lines

  1. /*   check_port.c        */
  2.  
  3. /*        Copyright © 1989 by Donald T. Meyer, Stormgate Software
  4.  *        All Rights Reserved
  5.  */
  6.  
  7. /*
  8.  *    Revised:
  9.  *    Andrew C. R. Martin        11.12.91  23:30
  10.  *    SciTech Software
  11.  *
  12.  *    The AllocMem(strlen(ARG0(...)),0L) seemed to be expanding the
  13.  *    ARG0() macro incorrectly causing 2 too few bytes to be allocated.
  14.  *    Hence MungWall hits!
  15.  *    Now uses an intermediate variable.
  16.  *    Changes are bracketed by ACRM+ and ACRM-
  17.  */
  18.  
  19.  
  20.  
  21. #include "rxil.h"
  22.  
  23. #include <string.h>
  24.  
  25.  
  26.  
  27. static void handle_message( struct RexxMsg *rexxmsg );
  28. static void handle_rexx_message( struct RexxMsg *rexxmsg );
  29. static void handle_return( struct RxilInvocation *rxi );
  30.  
  31. static int parse_inplace( char *p, char **argptr, int maxargs );
  32.  
  33.  
  34.  
  35. /*------------------------------------------------------------------*/
  36. /*                    Functions                                        */
  37. /*------------------------------------------------------------------*/
  38.  
  39. /* NAME
  40.  *        RxilCheckPort
  41.  *
  42.  * SYNOPSIS
  43.  *        RxilCheckPort();
  44.  *
  45.  * FUNCTION
  46.  *        Called by the client program's eventloop upon recieving a message
  47.  *        at one of the ports.
  48.  *        This handles the dispatching ( via RxilDispatch() ) of ARexx
  49.  *        commands, the launching of "looped back" macros, and receiving
  50.  *        the replys to macro invocation messages which we have sent to
  51.  *        Rexxmaster.
  52.  *
  53.  *        This is a "safe" call in that if the ARexx library was unopened
  54.  *        or the port opening failed, nothing will go boom!
  55.  *
  56.  * INPUTS
  57.  *        None
  58.  *
  59.  * RESULT
  60.  *        None
  61.  *
  62.  * SIDES
  63.  *
  64.  * HISTORY
  65.  *        01-Aug-89    Creation.
  66.  *        18-Nov-89    Changed an (incorrect) error return code in line 212.
  67.  *
  68.  * BUGS
  69.  *
  70.  * SEE ALSO
  71.  *        RxilInit(), RxilDispatch()
  72.  */
  73.  
  74. void RxilCheckPort( void )
  75. {
  76.     struct RexxMsg *rexxmsg;
  77.  
  78.  
  79.     /* Make call "safe" even if RxilInit() failed */
  80.     if( global_rdef == NULL )
  81.     {
  82.         return;
  83.     }
  84.  
  85.  
  86.     if( RxilCheckCancel() == TRUE )
  87.     {
  88.         /* It would appear that a cancel requester was posted, and
  89.          * the user clicked it!
  90.          */
  91.         RxilCancel();
  92.     }
  93.  
  94.  
  95.     if( global_rdef->SecretPort )
  96.     {
  97.         while(  rexxmsg = (struct RexxMsg *)
  98.             GetMsg( global_rdef->SecretPort )  )
  99.         {
  100.             global_rdef->FromRexx = RXIL_SECRET;
  101.             handle_message( rexxmsg );
  102.             global_rdef->FromRexx = 0;
  103.         }
  104.     }
  105.  
  106.     if( global_rdef->PublicPort )
  107.     {
  108.         while( rexxmsg = (struct RexxMsg *)
  109.             GetMsg( global_rdef->PublicPort ) )
  110.         {
  111.             global_rdef->FromRexx = RXIL_PUBLIC;
  112.             handle_message( rexxmsg );
  113.             global_rdef->FromRexx = 0;
  114.         }
  115.     }
  116.  
  117.  
  118.     /* Do we want to clear the Abort flag? */
  119.  
  120.     if( RxilPending() == FALSE )
  121.     {
  122.         if( global_rdef->CReq != NULL )
  123.         {
  124.             RxilEndCancel();
  125.         }
  126.  
  127.         if(  FlagIsClear( global_rdef->Flags, RXIL_NO_CLEARABORT )  )
  128.         {
  129.             global_rdef->Abort = FALSE;
  130.         }
  131.     }
  132. }
  133.  
  134.  
  135.  
  136. /* This routine is called with the message received at any rexx port.
  137.  * These messages will be of two broad classes.
  138.  * The first is a reply from a command or function which we invoked
  139.  * by sending a packet to RexxMaster.
  140.  *
  141.  * The second is a command sent to our application by an ARexx
  142.  * program.  This could potentialy be from the public port or from
  143.  * the uniquely-named private port.
  144.  */
  145.  
  146. static void handle_message( struct RexxMsg *rexxmsg )
  147. {
  148.     struct RxilInvocation *rxi;
  149.  
  150.  
  151.     /* Decide if it is a reply */
  152.  
  153.     for( rxi=global_rdef->Invocations; rxi; rxi=rxi->Next )
  154.     {
  155.         /* Is this an invocation reply? */
  156.         if( rexxmsg == rxi->RexxMsg )
  157.         {
  158.             /* Yes */
  159.             handle_return( rxi );
  160.             return;
  161.         }
  162.     }
  163.  
  164.  
  165.     /* Apparently not a completion message.  Should be a command
  166.      * from a Rexx program.
  167.      */
  168.  
  169.     if( IsRexxMsg(rexxmsg) == FALSE )
  170.     {
  171.         /* This message is not from ARexx.  Don't touch it,
  172.          * just Reply() it and return.
  173.          */
  174.         ReplyMsg( (struct Message *)rexxmsg );
  175.         return;
  176.     }
  177.  
  178.  
  179.     /* The message is from an ARexx program */
  180.  
  181.     handle_rexx_message( rexxmsg );
  182. }
  183.  
  184.  
  185.  
  186. /* This will take a command message received from an ARexx program
  187.  * and handle the dispatching and replying.
  188.  * This message MUST be from an ARexx program!
  189.  */
  190.  
  191. static void handle_rexx_message( struct RexxMsg *rexxmsg )
  192. {
  193.     char *cmdbuf;
  194.     struct RxilInvocation *child_rxi;
  195.  
  196. /* ACRM+ */
  197.    long memsize;
  198. /* ACRM- */
  199.  
  200.     /* Default to okay code. */
  201.     rexxmsg->rm_Result1 = RC_OK;
  202.     rexxmsg->rm_Result2 = 0;
  203.  
  204.  
  205.     /* Are we still open for business? */
  206.     if( global_rdef->Abort == TRUE )
  207.     {
  208.         /* Nope! */
  209.         rexxmsg->rm_Result1 = RXERR_ABORTED;
  210.         ReplyMsg( (struct Message *)rexxmsg );
  211.         return;
  212.     }
  213.  
  214.  
  215.     /* Handle the message contents */
  216.  
  217.     /* Just to be squeaky clean, since Bill says to affect only the
  218.      * result fields, copy this into a buffer before parsing.
  219.      *
  220.      * NOTE: Even though an Argstring can by definition contain    
  221.      * embedded null's, the command line is treated as though it
  222.      * were null terminated.
  223.      */
  224.  
  225. /* ACRM+
  226. //    cmdbuf = AllocMem(  strlen( ARG0(rexxmsg) )+1, 0  );
  227. */
  228.    memsize = strlen(rexxmsg->rm_Args[0])+1;
  229.     cmdbuf = AllocMem(  memsize, 0L );
  230. /* ACRM- */
  231.  
  232.     if( cmdbuf == NULL )
  233.     {
  234.         rexxmsg->rm_Result1 = RXERR_NO_MEMORY;
  235.         return;
  236.     }
  237.  
  238.     strcpy( cmdbuf, ARG0(rexxmsg) );
  239.  
  240.     /* Parse the command string */
  241.     global_rdef->ArgCount =
  242.         parse_inplace( cmdbuf, &(global_rdef->Arg[0]), RXIL_MAX_ARGS );
  243.  
  244.     if( global_rdef->ArgCount == 0 )
  245.     {
  246.         rexxmsg->rm_Result1 = RXERR_UNKNOWN_CMD;
  247.  
  248. /* ACRM+
  249. //        FreeMem(  cmdbuf, strlen( ARG0(rexxmsg) )  );
  250. */
  251.         FreeMem(  (void *)cmdbuf, memsize  );
  252. /* ACRM- */
  253.  
  254.         return;
  255.     }
  256.  
  257.     RxilDispatch( rexxmsg, global_rdef->Arg[0] );
  258.  
  259. /* ACRM+
  260. // FreeMem(  cmdbuf, strlen( ARG0(rexxmsg)+1 )  );
  261. */
  262.     FreeMem(  (void *)cmdbuf, memsize  );
  263. /* ACRM- */
  264.  
  265.     /* Look at macro loopback */
  266.  
  267.     if(  ( rexxmsg->rm_Result1 == RXERR_UNKNOWN_CMD ) &&
  268.         FlagIsClear( global_rdef->Flags, RXIL_NOLAUNCH )  )
  269.     {
  270.         /* Unknown command.  Try to launch it as an ARexx macro. */
  271.         child_rxi = RxilCreateRxi(  ARG0(rexxmsg), RXCOMM  );
  272.  
  273.         if( child_rxi != NULL )
  274.         {
  275.             if(  FlagIsSet( rexxmsg->rm_Action, RXFF_RESULT )  )
  276.             {
  277.                 SetFlag( child_rxi->ActionFlags, RXFF_RESULT );
  278.             }
  279.  
  280.             child_rxi->Parent = rexxmsg;
  281.  
  282.             if(  RxilLaunch( child_rxi ) == 0  )
  283.             {
  284.                 /* Launched successfully */
  285.                 return;            /* Return without replying */
  286.             }
  287.             else
  288.             {
  289.                 /* Launch failure */
  290.                 RxilDeleteRxi( child_rxi );
  291.                 rexxmsg->rm_Result1 = RXERR_FAILED;
  292.             }
  293.         }
  294.         else
  295.         {
  296.             /* Unable to create RxilInvocation structure */
  297.             rexxmsg->rm_Result1 = RXERR_NO_MEMORY;
  298.         }
  299.     }
  300.  
  301.  
  302.     RxilCheckResult( rexxmsg );
  303.  
  304.     ReplyMsg( (struct Message *)rexxmsg );
  305. }
  306.  
  307.  
  308.  
  309. static void handle_return( struct RxilInvocation *rxi )
  310. {
  311.     rxi->State = RXIL_STATE_RETURNED;
  312.  
  313.     if( rxi->CommAddr == global_rdef->SecretPortName )
  314.     {
  315.         /* This implies that the locked flag was set while
  316.          * this ran.  Decrement the lock count.
  317.          */
  318.         --(global_rdef->LockCount);
  319.     }
  320.  
  321.  
  322.     if( rxi->Parent == NULL )
  323.     {
  324.         /* This was originated by the client.  Let the client
  325.          * worry about handling the return and doing the cleanup.
  326.          */
  327.         return;
  328.     }
  329.  
  330.  
  331.     /* This is the return for a program which was "looped" back out
  332.      * to ARexx.
  333.      */
  334.  
  335.     /* Pass thru?? the result */
  336.  
  337.     rxi->Parent->rm_Result1 = rxi->RexxMsg->rm_Result1;
  338.     rxi->Parent->rm_Result2 = rxi->RexxMsg->rm_Result2;
  339.  
  340.     RxilCheckResult( rxi->Parent );
  341.  
  342.  
  343.     /* Reply to the parent */
  344.     ReplyMsg(  (struct Message *)(rxi->Parent)  );
  345.  
  346.  
  347.     /* Cleanup the invocation RexxMsg */
  348.  
  349.     /* Zero these to prevent the cleanup routine from freeing
  350.      * the return Argstring.
  351.      */
  352.     rxi->RexxMsg->rm_Result1 = 0;
  353.     rxi->RexxMsg->rm_Result2 = 0;
  354.  
  355.     /* Clear these to prevent the cleanup routine from closing
  356.      * streams which were inherited from the parent.
  357.      */
  358.     rxi->RexxMsg->rm_Stdin = NULL;
  359.     rxi->RexxMsg->rm_Stdout = NULL;
  360.  
  361.     RxilCleanupReturn( rxi );
  362.  
  363.  
  364.     /* And free the RxilInvocation structure */
  365.     RxilDeleteRxi( rxi );
  366. }
  367.  
  368.  
  369.  
  370. /* This is intended to be a very memory efficient parsing procedure.
  371.  * The length of the options is limited only by the length of the
  372.  * input line buffer (owned by the caller).  The number of arguments which
  373.  * can be parsed out of an input string is limited by the size of the
  374.  * pointer array, which is also owned by the caller.
  375.  * Note that this WILL MODIFY the contents of the line passed in the
  376.  * buffer.  If this is un-desirable, copy the string to a scratch buffer
  377.  * and pass that.
  378.  *
  379.  * The calling should look something like:
  380.  *        char *myargpointers[10];
  381.  *        int myargcount;
  382.  *        char buf[100];
  383.  *
  384.  *        gets( buf );
  385.  *        myargcount = inplace_parse( buf, myargpointers, 10 );
  386.  */
  387.  
  388. static int parse_inplace( char *p, char **argptr, int maxargs )
  389. {
  390.     BOOL inparen = FALSE, inarg = FALSE;
  391.     char *argstart = 0;        /* Does not really need to be initialized */
  392.     int argcount = 0;
  393.  
  394.  
  395.     while(  (*p != '\0') && (argcount < maxargs)  )
  396.     {
  397.         switch( *p )
  398.         {
  399.             case ' ':
  400.             case '\t':
  401.                 /* Whitespace */
  402.                 if(  (inarg == TRUE) && (inparen == FALSE)  )
  403.                 {
  404.                     /* We got one! */
  405.                     *p = '\0';        /* place a delimiting null */
  406.                     argptr[argcount++] = argstart;
  407.                     inarg = FALSE;
  408.                     inparen = FALSE;
  409.                 }
  410.                 break;
  411.  
  412.             case '"':
  413.                 if( inparen == TRUE )
  414.                 {
  415.                     /* This is closing paren */
  416.                     /* We got one! */
  417.                     *p = '\0';        /* place a delimiting null */
  418.                     argptr[argcount++] = argstart;
  419.                     inarg = FALSE;
  420.                     inparen = FALSE;
  421.                 }
  422.                 else
  423.                 {
  424.                     /* Opening paren */
  425.                     if( inarg == TRUE )
  426.                     {
  427.                         /* Deal with the previous arg first */
  428.                         *p = '\0';        /* place a delimiting null */
  429.                         argptr[argcount++] = argstart;
  430.                     }
  431.  
  432.                     inparen = TRUE;
  433.                     inarg = TRUE;
  434.                     argstart = p+1;
  435.                 }
  436.                 break;
  437.  
  438.             default:
  439.                 /* A character we want in an argument string */
  440.                 if( inarg == FALSE )
  441.                 {
  442.                     inarg = TRUE;
  443.                     argstart = p;
  444.                 }
  445.                 break;
  446.         }
  447.  
  448.         p++;
  449.     }
  450.  
  451.     if(  (inarg == TRUE) && (argcount < maxargs)  )
  452.         argptr[argcount++] = argstart;
  453.  
  454.     return( argcount );
  455. }
  456.  
  457.